package com.atguigu.flink.chapter05.sink;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
import org.apache.flink.connector.jdbc.JdbcExecutionOptions;
import org.apache.flink.connector.jdbc.JdbcSink;
import org.apache.flink.connector.jdbc.JdbcStatementBuilder;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author lzc
 * @Date 2022/7/5 10:19
 */
public class Flink05_Sink_Jdbc {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        
        env
            .socketTextStream("hadoop162", 9999)
            .map(new MapFunction<String, WaterSensor>() {
                @Override
                public WaterSensor map(String value) throws Exception {
                    String[] data = value.split(",");
                    
                    
                    return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
                }
            })
            .keyBy(WaterSensor::getId)
            .sum("vc")
            .addSink(
                JdbcSink.sink("replace into sensor values(?,?,?)",
                              new JdbcStatementBuilder<WaterSensor>() {
                                  @Override
                                  public void accept(PreparedStatement ps,
                                                     WaterSensor value) throws SQLException {
                                      ps.setString(1, value.getId());
                                      ps.setLong(2, value.getTs());
                                      ps.setInt(3, value.getVc());
                                  }
                              },
                              new JdbcExecutionOptions.Builder()
                                  .withBatchIntervalMs(2000)
                                  .withMaxRetries(3)
                                  .withBatchSize(4096)
                                  .build(),
                              new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
                                  .withDriverName("com.mysql.jdbc.Driver")
                                  .withUrl("jdbc:mysql://hadoop162:3306/test?useSSL=false")
                                  .withUsername("root")
                                  .withPassword("aaaaaa")
                                  .build()
                ));
        
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
}




/*
 
 */